package com.yaozheng.vocationaltraining.adapter;

import net.sf.json.JSONArray;

import com.yaozheng.vocationaltraining.fragment.ItemQuestionFragment;
import com.yaozheng.vocationaltraining.fragment.ItemQuestionFragment_;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

/**
 * 试卷题目列表
 *
 * @author Administrator
 */
public class PaperQuestionListAdapter extends FragmentStatePagerAdapter {
    JSONArray dataList;

    ItemQuestionFragment currentFragment;


    public PaperQuestionListAdapter(FragmentManager fm, JSONArray dataList) {
        super(fm);
        this.dataList = dataList;
    }

    public ItemQuestionFragment getCurrentFragment() {
        return currentFragment;
    }

    @Override
    public Fragment getItem(int position) {
        return newInstance(position);
    }

    public ItemQuestionFragment newInstance(int position) {
        ItemQuestionFragment questionFragment = ItemQuestionFragment_.builder()
                .build();
//        int currPosition = position % TypeUtils.getJsonArraySize(dataList);
        Bundle args = new Bundle();
        args.putInt("position", position);
        questionFragment.setItemData(TypeUtils.getJsonObject(dataList,
                position));
        questionFragment.setArguments(args);
        return questionFragment;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        currentFragment = (ItemQuestionFragment) object;
        super.setPrimaryItem(container, position, object);
    }

    @Override
    public int getCount() {

        if (TypeUtils.getJsonArraySize(dataList) > 0) {
            return TypeUtils.getJsonArraySize(dataList);//Integer.MAX_VALUE;
        }
        return 0;
    }

}
